package org.capybara.coffeehouse.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.capybara.coffeehouse.action.Action;
import org.capybara.coffeehouse.action.CommandListAction;
import org.capybara.coffeehouse.command.TurnLightOff;
import org.capybara.coffeehouse.command.TurnLightOn;
import org.capybara.coffeehouse.device.Device;
import org.capybara.coffeehouse.device.Insteon2457D2;
import org.capybara.coffeehouse.device.LightDimmer;
import org.capybara.coffeehouse.device.Switch;
import org.capybara.coffeehouse.event.Event;
import org.capybara.coffeehouse.event.TurnOffEvent;
import org.capybara.coffeehouse.event.TurnOnEvent;
import org.capybara.coffeehouse.insteon.InsteonAddress;
import org.capybara.coffeehouse.trigger.Trigger;


/**
 * Holds the system configuration.
 * @author dfraser
 *
 */
public class Configuration {
	private Map<String, Device> devices = new HashMap<>();
	private Map<String, Action> actions = new HashMap<>();
	private Map<String, Event> events = new HashMap<>();
	
	public Configuration() {
		
		devices.put("light1",new Insteon2457D2("light1", new InsteonAddress(0x14, 0x41, 0xbd)));
		devices.put("switch1", new Switch());
		
		// make a new action
		CommandListAction turnOnDiningRoomLight = new CommandListAction();
		turnOnDiningRoomLight.getCommands().add(new TurnLightOn((LightDimmer) devices.get("light1")));

		CommandListAction turnOffDiningRoomLight = new CommandListAction();
		turnOffDiningRoomLight.getCommands().add(new TurnLightOff((LightDimmer) devices.get("light1")));
		
		actions.put("turnOnDiningRoomLight", turnOnDiningRoomLight);
		actions.put("turnOffDiningRoomLight", turnOffDiningRoomLight);

		// make a new trigger
		List<String> cond;
		cond = new ArrayList<>();
		cond.add(TurnOnEvent.class.getName());
		Trigger diningRoomLightSwitchOn = new Trigger(cond,turnOnDiningRoomLight);
		devices.get("switch1").addEventListener(diningRoomLightSwitchOn);

		cond = new ArrayList<>();
		cond.add(TurnOffEvent.class.getName());
		Trigger diningRoomLightSwitchOff = new Trigger(cond,turnOffDiningRoomLight);
		devices.get("switch1").addEventListener(diningRoomLightSwitchOff);

	}
	
	public Device getDeviceByName(String name) {
		return devices.get(name);
	}
}
